package com.xjtu.platform.admin.impl;

import com.github.pagehelper.PageInfo;
import com.xjtu.platform.admin.BjyAdminService;
import com.xjtu.platform.admin.dao.BjyAdminDao;
import com.xjtu.platform.admin.dto.bjyDto.AddRuleDto;
import com.xjtu.platform.admin.dto.bjyDto.BorrowStaticDto;
import com.xjtu.platform.admin.entity.AdminBookBaseInfo;
import com.xjtu.platform.admin.entity.AdminBorrowHisEntity;
import com.xjtu.platform.admin.entity.AdminBorrowReaderInfo;
import com.xjtu.platform.admin.entity.PenaltyRule;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.entity.FileEntity;
import com.xjtu.platform.common.page.QueryAction2;
import com.xjtu.platform.reader.dao.ReaderDao;
import com.xjtu.platform.reader.entity.Author;
import com.xjtu.platform.reader.entity.BookBaseInfo;
import com.xjtu.platform.reader.entity.BorrowHisBaseInfo;
import com.xjtu.platform.reader.entity.PenaltyEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BjyAdminServiceImpl implements BjyAdminService {

    @Resource
    private BjyAdminDao bjyAdminDao;

    @Resource
    private ReaderDao readerDao;

    @Override
    public Map<String, Object> getBorrowStatic(BorrowStaticDto borrowStaticDto) {
        return Utils.getPage2(borrowStaticDto, new QueryAction2<AdminBorrowHisEntity>() {

            @Override
            public PageInfo<AdminBorrowHisEntity> execute() {
                List<BorrowHisBaseInfo> baseInfoList = bjyAdminDao.getAdminBorrowBaseInfoList(borrowStaticDto);
                PageInfo<BorrowHisBaseInfo> basePageInfo = new PageInfo<>(baseInfoList);

                List<AdminBorrowHisEntity> finalList = new ArrayList<>();
                for(BorrowHisBaseInfo item : baseInfoList){
                    AdminBorrowHisEntity adminBorrowHisEntity = new AdminBorrowHisEntity();
                    AdminBookBaseInfo adminBookBaseInfo = bjyAdminDao.getBookBaseInfo(item.getBook_id());


                    List<FileEntity> fileEntity = readerDao.getBookPic(item.getBook_id());
                    for(FileEntity s: fileEntity){
                        s.setFile_url(Utils.transAddToUrl(s.getFile_url()));
                    }
                    adminBorrowHisEntity.setFileEntities(fileEntity);


                    List<Author> authorList = readerDao.getBookAuthors(item.getBook_id());
                    adminBorrowHisEntity.setAuthorList(authorList);

                    AdminBorrowReaderInfo adminBorrowReaderInfo = bjyAdminDao.getReaderBorrowInfo(item.getReader_id());
                    adminBorrowHisEntity.setAdminBorrowReaderInfo(adminBorrowReaderInfo);

                    PenaltyEntity p = readerDao.getPenaltyById(item.getBook_reader_his_id());
                    if(p == null){
                        adminBorrowHisEntity.setPenalty(false);
                    }else {
                        adminBorrowHisEntity.setPenalty(true);
                    }
                    adminBorrowHisEntity.setPenaltyEntity(p);

                    adminBorrowHisEntity.setAdminBookBaseInfo(adminBookBaseInfo);


//                    adminBorrowHisEntity.setBookBaseInfo((BookBaseInfo) bookBaseInfo);
                    adminBorrowHisEntity.setBorrowHisBaseInfo(item);
                    finalList.add(adminBorrowHisEntity);
                }

                PageInfo<AdminBorrowHisEntity> page2 = new PageInfo<>();
                BeanUtils.copyProperties(basePageInfo,page2);
                page2.setList(finalList);
                return page2;
            }
        });
    }

    @Override
    public List<PenaltyRule> getPenaltyRule() {
        return readerDao.getPenaltyRule();
    }

    @Override
    public boolean addRule(AddRuleDto addRuleDto) {
        try {
            readerDao.addRule(addRuleDto);
            return true;
        } catch (Exception e) {
            e.printStackTrace();;
            return false;
        }
    }

    @Override
    public boolean deleteRole(String id) {
        try {
            readerDao.deleteRole(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();;
            return false;
        }
    }
}
